<?php

namespace App\Service\Index;

use App\Constants\ErrorCode;
use App\Model\CallBase;
use App\Model\GiftLog;
use App\Model\Message;
use App\Model\UserProfile;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use App\Service\BaseService;

/**
 * 通话检测(10-15秒调用一次)
 */
class CallCheckingService extends BaseService
{
    protected function RoVerifyList()
    {
        return [
            'chat_user_id' => 'required|int',
        ];
    }

    public function handle()
    {
        $userId = $this->getUserId();
        $chatUserId = $this->request->input('chat_user_id', 0);
        $callType = $this->request->input('call_type', 0);
        $cls = $callType == 1 ? VideoCall::class : VoiceCall::class;
        //根据频道ID查记录
        $record = $cls::checkRecord($userId, $chatUserId);
        $ret['result'] = 1;
        //通话记录不存在
        if (!$record) {
            return $this->setError(ErrorCode::PARAMETER_ERROR, '通话记录不存在');
        }
        //非通话中的状态不处理
        if ($record->status != CallBase::STATUS_ING) {
            // return $this->setError(ErrorCode::PARAMETER_ERROR, '非通话状态');
            Message::sendSlient($userId, 'HANGUP_CALL');
            log_user_result($userId, "CallId:{$record->id}==Hangup");
            return $ret;
        }
        //心跳字段
        // $heartbeatField = $userId == $record->user_id ? 'heartbeat_user' : 'heartbeat_chat';
        // $offTime = time() - $record->answer_time;
        //推送版本
        // $pushGiftVersion = $this->curVersion >= env('NEW_PAYWAY_VERSION', '1.3.7');
        //非常规通话
        // $noPushType = in_array($record->from, [CallBase::FROM_DAILY_FREE_HOST, CallBase::FROM_DAILY_FREE]);
        // if ($pushGiftVersion && !$noPushType && $offTime >= 30 && $offTime <= 36) {
        //     $profile = UserProfile::getProfile($userId);
        //     $giftId = intval(env('CALL_PUSH_GIFT_ID', 63));
        //     if (
        //         UserProfile::checkRoleHost($profile->role)
        //         && !GiftLog::getSendLog(
        //             $userId,
        //             $chatUserId,
        //             GiftLog::FROM_VIDEO_CALL_FREE,
        //             date('Y-m-d')
        //         )
        //     ) {
        //         Message::sendVideoGiftTip($userId, $giftId);
        //     }
        // }
        //修改通话时间
        $record = $cls::updateCallTime($record, $userId);
        $ret['result'] = $record->id;
        log_user_result($userId, "CallId:{$record->id}==CallType:{$callType}==OffTime:{$offTime}");
        return $ret;
    }
}
